package com.jason.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Number;
import com.jason.system.service.NumberService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.bouncycastle.util.Integers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/25 17:08
 */
@RestController
@RequestMapping("/api/number")
@Api(value = "/number",tags = "电话信息接口")
public class NumberController {

  private static final Logger logger = LoggerFactory.getLogger(NumberController.class);

  @Autowired
  private NumberService numberService;

  @ApiOperation(value = "电话信息",notes = "获取所有电话信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page", value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "没页个数",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200 , message = "获取电话信息成功"),
          @ApiResponse(code = 201, message = "获取电话信息失败")
  })
  @GetMapping("/getAll")
  public JSONObject getAllNumbers(@RequestParam("page") Integer page ,@RequestParam("size") Integer size){
    logger.info("==>>>numberController==>>>进行路径：/api/number/getAll.....");
    JSONObject jsonObject = new JSONObject();
    Pager<Number> numbers = numberService.getAllNumbers(page,size);
    logger.info("==>>>numberController==>>>获取numbers对象: {}",numbers);
    ResultEntity r = new ResultEntity();
    if (numbers!=null){
      r.setCode(200);
      r.setMessage("获取电话信息成功");
      r.setData(numbers);
    }else {
      r.setCode(201);
      r.setMessage("获取电话信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "添加电话",notes = "添加电话信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "consultant",value = "咨询人",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "number",value = "电话号码",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "stuName",value = "学生姓名，string形式，以逗号分割的多个学生姓名",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "time",value = "时间",required = true,dataType = "String")

  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "添加电话信息成功"),
          @ApiResponse(code = 201,message = "添加电话信息失败")
  })
  @PostMapping("/insert")
  public JSONObject insertNumber( Number insertNumber){
    logger.info("==>>>numberController==>>>进行路径：/api/number/insert.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    try {
      logger.info("==>>>numberController==>>>正在添加电话信息中.....");
      r.setCode(200);
      r.setMessage("添加电话成功");
      numberService.insertNumber(insertNumber);
      logger.info("==>>>numberController==>>>添加电话信息成功.....");
    }catch (Exception e){
      r.setCode(201);
      r.setMessage("添加电话失败");
      logger.info("==>>>numberController==>>>添加电话信息失败.....");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "修改电话",notes = "修改电话信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name ="nId",value = "电话id",required = true,dataType ="Integer"),
          @ApiImplicitParam(paramType = "query",name = "consultant",value = "咨询人",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "number",value = "电话号码",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "stuName",value = "学生姓名",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "time",value = "时间",required = true,dataType = "String")

  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "修改电话信息成功"),
          @ApiResponse(code = 201,message = "修改电话信息失败")
  })
  @PostMapping("/update")
  public JSONObject updateNumber( Number updateNumber){
    logger.info("==>>>numberController==>>>进行路径：/api/number/update.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>numberController==>>>正在更新number中.....");
    try {
      numberService.updateNumber(updateNumber);
      logger.info("==>>>numberController==>>>更新number完成.....");
      r.setCode(200);
      r.setMessage("修改电话信息成功");
    }catch (Exception e){
      logger.info("==>>>numberController==>>>更新number失败:{}",e.getMessage());
      r.setCode(201);
      r.setMessage("修改电话信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="删除电话",notes = "删除电话信息")
  @ApiImplicitParam(paramType = "query",name = "nId",value = "电话id",required = true,dataType = "Integer")
  @ApiResponses({
          @ApiResponse(code = 200,message = "删除电话信息成功"),
          @ApiResponse(code = 201,message = "删除电话信息失败")
  })
  @PostMapping("/delete/{nId}")
  public JSONObject delete(@PathVariable("nId") Integer nId){
    logger.info("==>>>numberController==>>>进行路径：nId:{}",nId);
    logger.info("==>>>numberController==>>>进行路径：/api/number/delete.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>numberController==>>>正在删除number中.....");
    boolean result = numberService.deleteNumber(nId);
    if (result){
      logger.info("==>>>numberController==>>>删除number完成.....");
      r.setCode(200);
      r.setMessage("删除电话信息成功");
    }else {
      logger.info("==>>>numberController==>>>删除number失败.....");
      r.setCode(201);
      r.setMessage("删除电话信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="批量删除电话",notes = "批量删除电话信息")
  @ApiImplicitParam(paramType = "query",name = "nIds",value = "电话ids",required = true,dataType = "String")
  @ApiResponses({
          @ApiResponse(code = 200,message = "批量删除电话信息成功"),
          @ApiResponse(code = 201,message = "批量删除电话信息失败")
  })
  @PostMapping("/deleteNumbers")
  public JSONObject deleteNumbers(String nIds){
    logger.info("==>>>numberController==>>>进行路径：/api/number/deleteNumbers.....");
    logger.info("==>>>numberController==>>>nIds:{}",nIds);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>numberController==>>>正在删除numbers中.....");
    boolean result = numberService.deleteNumbers(nIds);
    if (result){
      logger.info("==>>>numberController==>>>批量删除numbers完成.....");
      r.setCode(200);
      r.setMessage("批量删除电话信息成功");
    }else {
      logger.info("==>>>numberController==>>>批量删除numbers失败.....");
      r.setCode(201);
      r.setMessage("批量删除电话信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @PostMapping("/search")
  public JSONObject searchNumbers(Integer page , Integer size , String address , String stuName){
    logger.info("==>>>numberController==>>>进行路径：/api/number/searchNumbers.....");
    logger.info("==>>>numberController==>>>address:{}",address);
    logger.info("==>>>numberController==>>>stuName:{}",stuName);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>numberController==>>>正在查询numbers中.....");
    Pager<Number> numbers = numberService.searchNumbers(page,size, address,stuName);
    logger.info("==>>>numberController==>>>numbers:{}",numbers);
    if (numbers!=null){
      logger.info("==>>>numberController==>>>查询numbers完成.....");
      r.setCode(200);
      r.setMessage("查询电话信息成功");
      r.setData(numbers);
    }else {
      logger.info("==>>>numberController==>>>查询numbers失败.....");
      r.setCode(201);
      r.setMessage("查询电话信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }
}
